package org.fog.json.map;

import org.fog.json.common.JsonFieldType;
import org.fog.json.common.JsonType;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * User: 缪凯
 * Date: 12-2-3
 * Time: 下午5:30
 */
public class JsonMapBodyImp implements JsonMapBody {
    private String id = null;
    private JsonType type = null;
    private JsonMapPackage jsonMapPackage = null;

    //所有属性
    private CopyOnWriteArrayList<JsonMapField> basic = null;
    private ConcurrentHashMap<String , JsonMapField> basicMaps = null;

    //映射属性
    private CopyOnWriteArrayList<JsonMapField> reflex = null;
    private ConcurrentHashMap<String , JsonMapField> reflexMaps = null;


    public JsonMapBodyImp(String id, JsonType type, JsonMapPackage jsonMapPackage){
        new JsonMapBodyImp(id , type , jsonMapPackage, null);
    }

    public JsonMapBodyImp
            (String id, JsonType type, JsonMapPackage jsonMapPackage,
             JsonMapField[] fields){
        if(id != null && type != null && jsonMapPackage != null){
            this.id = id;
            this.type = type;
            this.jsonMapPackage = jsonMapPackage;
            this.basicMaps = new ConcurrentHashMap<String, JsonMapField>();
            this.reflexMaps = new ConcurrentHashMap<String, JsonMapField>();

            if(fields != null && fields.length > 0)
                this.loadFields(fields);
        }
    }

    /*
    装载所有属性与映射属性
     */
    private void loadFields(JsonMapField[] fields){
        this.basic = new CopyOnWriteArrayList<JsonMapField>(fields);
        this.reflex = new CopyOnWriteArrayList<JsonMapField>();

        for(JsonMapField field : fields){
            this.basicMaps.put(field.getName(), field);
            //当属性为反射时构造
            if(field.getType() == JsonFieldType.Reflex){
                this.reflexMaps.put(field.getName() , field);
                this.reflex.add(field);
            }
        }
    }


    public JsonMapCube getCube() {
        if(this.jsonMapPackage != null)
            return this.jsonMapPackage.getCube();
        return null;
    }

    public JsonMapPackage getPackage() {
        return this.jsonMapPackage;
    }

    public JsonType getBodyType() {
        return this.type;
    }

    public String getBodyId() {
        return this.id;
    }

    public int getFieldSize() {
        if(this.basic != null){
            return this.basic.size();
        }
        return 0;
    }

    public JsonMapField getField(int location) {
        if(this.basic != null && location >= 0 && location < this.basic.size())
            return this.basic.get(location);
        return null;
    }

    public JsonMapField getField(String name) {
        if(this.basicMaps != null && name != null && !"".equals(name))
            return this.basicMaps.get(name);
        return null;
    }

    public List<JsonMapField> getFields() {
        return this.basic;
    }

    public JsonMapField getRefFields(String name) {
        if(this.reflexMaps != null && name != null && !"".equals(name))
            return this.reflexMaps.get(name);
        return null;
    }

    public List<JsonMapField> getRefFields() {
        return this.reflex;
    }
}
